package net.xxcxcxcx.xc_enhancedmob.render.damage_num;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.Vec3d;

import java.util.Random;

//Particle to show damage(heal) number
@Environment(value = EnvType.CLIENT)
public class DamageNumParticle {
    public static Random RAND = new Random();
    public float damage;
    public double x = 0.0D;
    public double y = 0.0D;
    public double z = 0.0D;
    public double xPrev = 0.0D;
    public double yPrev = 0.0D;
    public double zPrev = 0.0D;
    public int age = 0;
    public double ax = 0.0D;
    public double ay = -0.01D;
    public double az = 0.0D;
    public double vx = 0.0D;
    public double vy = 0.0D;
    public double vz = 0.0D;
    public String damageType;

    public DamageNumParticle(Entity entity, float damage, String damageSource) {
        MinecraftClient client = MinecraftClient.getInstance();
        Vec3d entityLocation = entity.getPos().add(0.0D, entity.getHeight(), 0.0D);
        Vec3d cameraLocation = client.gameRenderer.getCamera().getPos();
        double offsetBy = entity.getWidth();
        Vec3d offset = cameraLocation.subtract(entityLocation).normalize().multiply(offsetBy);
        Vec3d pos = entityLocation.add(offset);
        this.damageType = damageSource;
        this.age = 0;
        this.damage = damage;
        this.vx = RAND.nextGaussian() * 0.04D;
        this.vy = 0.1D + RAND.nextGaussian() * 0.05D;
        this.vz = RAND.nextGaussian() * 0.04D;
        this.x = pos.x;
        this.y = pos.y;
        this.z = pos.z;
        this.xPrev = this.x;
        this.yPrev = this.y;
        this.zPrev = this.z;
    }


    public void tick() {
        this.xPrev = this.x;
        this.yPrev = this.y;
        this.zPrev = this.z;
        ++this.age;
        this.x += this.vx;
        this.y += this.vy;
        this.z += this.vz;
        this.vx += this.ax;
        this.vy += this.ay;
        this.vz += this.az;
    }
}
